<?php
namespace app\index\controller;
use think\Db;
use app\common\MemcacheOperate;
class Index extends Common
{
    public $mem;

    //首页
    public function index()
    {
        require_once('./index.shtml');
    }

    //伪首页,供index_crontab方法抓取
    public function index_buffer()
    {
        //轮播图
        $banners=DB::name('banner')->where('status',1)->order('displayorder desc')->select();
        foreach($banners as $key=>$value)
        {
            $banners[$key]['pic']=URL.$value['pic'];
        }
        //视频
        $video=DB::name('article')->where('cid',29)->order('displayorder desc')->find();

        //babyblue动态
        $updatings=DB::name('article')->where('cid',3)->where('status',1)->order('displayorder desc')->limit(8)->select();

        //会员中心
        $vip=DB::name('article')->where('cid',21)->where('status',1)->order('displayorder desc')->limit(5)->select();

        //游戏化评估
        $game=DB::name('article')->where('cid',20)->where('status',1)->order('displayorder desc')->limit(5)->select();

        //成长记录
        $grow=DB::name('article')->where('cid',19)->where('status',1)->order('displayorder desc')->limit(5)->select();

        $this->assign('vip',$vip);
        $this->assign('game',$game);
        $this->assign('grow',$grow);
        $this->assign('updatings',$updatings);
        $this->assign('video',$video);
        $this->assign('banners',$banners);
        return $this->fetch('index');
    }

    //定期抓取首页,写入index.shtml文件
    public function index_crontab()
    {
        $contents=file_get_contents(URL.'/index/index/index_buffer');
        //将静态文件放到public下的index.shtml中
        $res=file_put_contents('./index.shtml',$contents);
        if($res){
            echo '缓存写入成功';
        }
    }

    //微信机器人识别二维码
    public  function qrcode()
    {
        $filename=input('filename');
        $f=file_get_contents('http://vbot.baolanbb.com/qrcode/'.$filename.'.jpg');
        file_put_contents('/www/babyblue/public/static/index/image/'.$filename.'.jpg',$f);
        //新建一个图像对象
        $image = new \ZBarCodeImage('/www/babyblue/public/static/index/image/'.$filename.'.jpg');

        // 创建一个二维码识别器
        $scanner = new \ZBarCodeScanner();

        //识别图像
        $barcode = $scanner->scan($image);

        //循环输出二维码信息
        if (!empty($barcode)) {
            foreach ($barcode as $code) {
//                printf("Found type %s barcode with data %s\n", $code['type'], $code['data']);
                return $code['data'];
            }
        }
    }
}
